package com.citrinus.feedme;

import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.citrinus.feedme.places.Place;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public final class MapFragment extends SherlockFragment implements GoogleMap.InfoWindowAdapter {

	private static final String TAG = MapFragment.class.getSimpleName();
	public final static int GOOGLEPLAY_REQUEST_CODE = 3;

	private View rootView;
	private GoogleMap googleMap;
	private HashMap<String, Marker> markerList;
	private MarkerOptions userPos;
	private TextView customMessage;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_map, container, false);
		customMessage = (TextView) rootView.findViewById(R.id.customMessage);
		setMap();
		setUserMarker();

		markerList = new HashMap<String, Marker>();
		return rootView;
	}

	private void setUserMarker() {
		userPos = new MarkerOptions();
		userPos.position(new LatLng(0, 0));
		// markerOptions.title(place.getName());
		userPos.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_maps_indicator_current_position_anim1));
		// markerOptions.snippet(place.getVicinity()+" - "+place.getDistanceInYards());
	}

	private void setMap() {
		try {
			SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.google_map);
			googleMap = mapFragment.getMap();
			// googleMap.setMyLocationEnabled(true);
		} catch (Exception exception) {
			Log.w(TAG, "GoogleMap unsupported.");
			googleMap = null;
		}
	}

	public void updateData(LatLng point) {
		// clean previous places
		googleMap.clear();

		// center in location
		center(point);

		// paint restaurants
		List<Place> places = App.instance.getDb().getNearPlaces(point);
		for (Place place : places) {
			addMarker(place);
		}
	}

	private void addMarker(Place place) {
		MarkerOptions markerOptions = new MarkerOptions();
		markerOptions.position(place.getGeometry().getLocation().getLatLng());
		markerOptions.title(place.getName());
		markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_ico));
		markerOptions.snippet(place.getVicinity() + " - " + place.getDistanceInYards());
		Marker m = googleMap.addMarker(markerOptions);

		markerList.put(place.getId(), m);
	}

	public void centerInLocal(Place p) {
		CameraUpdate center = CameraUpdateFactory.newLatLng(p.getGeometry().getLocation().getLatLng());
		CameraUpdate zoom = CameraUpdateFactory.zoomTo(18);
		googleMap.moveCamera(center);
		googleMap.animateCamera(zoom);

		Marker m = markerList.get(p.getId());
		m.showInfoWindow();

	}

	public void center(LatLng position) {
		userPos.position(position);
		googleMap.addMarker(userPos);
		CameraUpdate center = CameraUpdateFactory.newLatLng(position);
		CameraUpdate zoom = CameraUpdateFactory.zoomTo(18);
		googleMap.moveCamera(center);
		googleMap.animateCamera(zoom);
	}

	@Override
	public View getInfoContents(Marker m) {
		// Getting view from the layout file info_window_layout

		View v = LayoutInflater.from(getActivity()).inflate(R.layout.info_contents, null);

		TextView title = (TextView) v.findViewById(R.id.title);
		title.setText(m.getTitle());
		title.setTypeface(App.getRobot());

		TextView subtitle = (TextView) v.findViewById(R.id.subtitle);
		subtitle.setText(m.getSnippet());
		subtitle.setTypeface(App.getRobot());

		return v;

	}

	@Override
	public View getInfoWindow(Marker arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setCustomMessage(int res) {
		customMessage.setVisibility(View.VISIBLE);
		customMessage.setText(res);
	}
	
	public void setCustomMessage(String text) {
		customMessage.setVisibility(View.VISIBLE);
		customMessage.setText(text);
	}
	
	public void hideCustomMessage() {
		customMessage.setVisibility(View.INVISIBLE);
	}

}
